//
//  ViewController.m
//  Lie
//
//  Created by John on 2017/6/9.
//  Copyright © 2017年 HR Inc. All rights reserved.
//

#import "ViewController.h"
#import "Charts/Charts.h"

@interface ViewController ()<ChartViewDelegate>

@property (nonatomic, strong) LineChartView *lineChartView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    BarChartView *chatView = [[BarChartView alloc] initWithFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, [UIScreen mainScreen].bounds.size.height - 10)];
//    [self.view addSubview:chatView];
    
    self.lineChartView = [[LineChartView alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width,250)];
    [self.view addSubview:self.lineChartView];
    self.lineChartView.delegate = self;
    self.lineChartView.backgroundColor =  [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1];
    self.lineChartView.noDataText = @"暂无数据";
    
    
    self.lineChartView.chartDescription.enabled = NO; //表格详情
    self.lineChartView.scaleYEnabled = NO;//取消Y轴缩放
    self.lineChartView.dragEnabled = NO; //启用拖拽图标
    [self.lineChartView setScaleEnabled:NO]; //是否缩放
    self.lineChartView.pinchZoomEnabled = NO;
    self.lineChartView.drawGridBackgroundEnabled = NO; //表格背景
    
//    self.lineChartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
//    self.lineChartView.xAxis.gridLineDashPhase = 0.f;
    
    //x轴设置
    ChartXAxis *xAxis = self.lineChartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelTextColor = [UIColor blackColor];
    xAxis.labelFont = [UIFont systemFontOfSize:12];
    xAxis.gridColor = [UIColor clearColor];
    xAxis.gridLineWidth = 1.f;
    xAxis.drawGridLinesEnabled = NO;//网络线
    
    
    // y轴设置
    ChartYAxis *leftAxis = self.lineChartView.leftAxis;
    leftAxis.axisMaximum = 100.0;
    leftAxis.axisMinimum = 0.0;
    leftAxis.labelTextColor = [UIColor blackColor];
    leftAxis.labelFont = [UIFont systemFontOfSize:12];
    leftAxis.gridColor = [UIColor blackColor];
    leftAxis.gridLineWidth = 1.f;
    
    leftAxis.gridLineDashLengths = @[@10.0,@10.0];
    leftAxis.gridLineDashPhase = 0.f;
    
    //网格线
    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    leftAxis.drawZeroLineEnabled = NO;
    leftAxis.drawLimitLinesBehindDataEnabled = NO;
    
    self.lineChartView.rightAxis.enabled = NO;
    [self setDataCount:7 range:80];
    
}

//设置数据源

- (void)setDataCount:(int)count range:(double)range
{
    NSMutableArray *values = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double val = arc4random_uniform(range) + 3;
        [values addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@""]]];
        
    }
    
    LineChartDataSet *set1 = nil;
    if (self.lineChartView.data.dataSetCount > 0)
    {
        set1 = (LineChartDataSet *)self.lineChartView.data.dataSets[0];
        set1.values = values;
        [self.lineChartView.data notifyDataChanged];
        [self.lineChartView notifyDataSetChanged];
    }
    
    else
    {
        // ------折线   引用 xAliis 、 yAliis 数组的值
        
        set1 = [[LineChartDataSet alloc] initWithValues:values label:@""];
        set1.drawIconsEnabled = NO;
        set1.drawSteppedEnabled = NO;
        
        
   //     set1.lineDashLengths = @[@5.f, @2.5f];
   //     set1.highlightLineDashLengths = @[@5.f, @2.5f];
        [set1 setColor:UIColor.blackColor];
        [set1 setCircleColor:UIColor.blackColor];
        
        set1.lineWidth = 2.0;
        set1.circleRadius = 3.0;
        set1.drawCircleHoleEnabled = NO;
        set1.valueFont = [UIFont systemFontOfSize:9.f];
     //   set1.formLineDashLengths = @[@5.f, @2.5f];
        set1.formLineWidth = 2.0;
        set1.formSize = 15.0;
        
        // ------颜色渲染区域
        
        NSArray *gradientColors = @[
                                    (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
                                    (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
                                    ];
        
        CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
        
        set1.fillAlpha = 1.f;
        set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
        set1.drawFilledEnabled = YES;
        
        CGGradientRelease(gradient);
        
        // ------数据源
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
        self.lineChartView.data = data;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
